use PlPN qw(PN);
use strict;

=pod

Sort: sorts the selected lines

Upper case: converts the selected text to upper-case

Lower case: converts the selected text to lower-case

=cut

PlPN->RegisterScript('example', 'Sort', sub {
	my $doc = PN->GetCurrentDocument;
	
	# step 1: get the start of the first line

	# SCI_GETSELECTIONSTART = 2143;
	my $start = $doc->SendEditorMessage(2143);
	
	# SCI_LINEFROMPOSITION = 2166;
	my $first = $doc->SendEditorMessage(2166, $start);

	# SCI_POSITIONFROMLINE = 2167;
	my $newstart = $doc->SendEditorMessage(2167, $first);
	
	# step 2: get the end of the last line

	# SCI_GETSELECTIONEND = 2145;
	my $end = $doc->SendEditorMessage(2145);
	
	# SCI_LINEFROMPOSITION = 2166;
	my $last = $doc->SendEditorMessage(2166, $end);

	# SCI_GETLINEENDPOSITION 2136 = 2136;
#	my $newend = $doc->SendEditorMessage(2136, $last);
	
	# actually, let's get the start of the next line
	# so we get the line ending as well
	# SCI_POSITIONFROMLINE = 2167;
	my $newend = $doc->SendEditorMessage(2167, $last+1);
	
	# step 3: change the selection
	# if we set start before getting end, we get the wrong value
	# (aparently setting star also sets end)
	
	# SCI_SETSELECTIONSTART = 2142;
	$doc->SendEditorMessage(2142, $newstart);

	# SCI_SETSELECTIONEND = 2144;
	$doc->SendEditorMessage(2144, $newend);
	
	# step 4: get selected text
	
	# SCI_GETSELTEXT = 2161;
	my $length = $doc->SendEditorMessage(2161, 0, undef);
	
	# make sure the buffer is big enough
	my $lines = "\0" x $length;
	
	# SCI_GETSELTEXT = 2161;
	$doc->SendEditorMessage(2161, $length, $lines);
	
	# text is returned with two trailing nulls, one from Scintilla
	# and one that XS adds on input to make sure the string is null-terminated
	$lines=~s/\0+$//;
	
	# step 5: split and sort the text
	my @lines = $lines=~/^(.*?(?:\r\n?|\n))/gsm;
	@lines = sort @lines;
	my $newlines = join('', @lines);
	
	# or in one line:
#	my $newlines = join('', sort $lines=~/^(.*?(?:\r\n?|\n))/g);

	# step 5: replace the selection

	# SCI_REPLACESEL = 2170;
	$doc->SendEditorMessage(2170, 0, $newlines);
});

PlPN->RegisterScript('example', 'Upper Case', sub {
	my $doc = PN->GetCurrentDocument;
	
	# get selected text
	
	# SCI_GETSELTEXT = 2161;
	my $length = $doc->SendEditorMessage(2161, 0, undef);
	
	# make sure the buffer is big enough
	my $text = "\0" x $length;
	
	# SCI_GETSELTEXT = 2161;
	$doc->SendEditorMessage(2161, $length, $text);
	
	# text is returned with two trailing nulls, one from Scintilla
	# and one that XS adds on input to make sure the string is null-terminated
	$text=~s/\0+$//;

	# replace it

	# SCI_REPLACESEL = 2170;
	$doc->SendEditorMessage(2170, 0, uc $text);
});

PlPN->RegisterScript('example', 'Lower Case', sub {
	my $doc = PN->GetCurrentDocument;
	
	# get selected text
	
	# SCI_GETSELTEXT = 2161;
	my $length = $doc->SendEditorMessage(2161, 0, undef);
	
	# make sure the buffer is big enough
	my $text = "\0" x $length;
	
	# SCI_GETSELTEXT = 2161;
	$doc->SendEditorMessage(2161, $length, $text);
	
	# text is returned with two trailing nulls, one from Scintilla
	# and one that XS adds on input to make sure the string is null-terminated
	$text=~s/\0+$//;

	# replace it

	# SCI_REPLACESEL = 2170;
	$doc->SendEditorMessage(2170, 0, lc $text);
});
